package com.empire.module.spider.service.lotteryfootballmatchsporttery;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.empire.framework.common.util.date.LocalDateTimeUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import com.empire.module.spider.controller.app.lotteryfootballmatchsporttery.vo.*;
import com.empire.module.spider.dal.dataobject.lotteryfootballmatchsporttery.LotteryFootballMatchSportteryDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.spider.dal.mysql.lotteryfootballmatchsporttery.LotteryFootballMatchSportteryMapper;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.spider.enums.ErrorCodeConstants.*;

/**
 * 足球竞猜 Service 实现类
 *
 * @author 无敌
 */
@Service
@Validated
public class LotteryFootballMatchSportteryServiceImpl implements LotteryFootballMatchSportteryService {

    @Resource
    private LotteryFootballMatchSportteryMapper lotteryFootballMatchSportteryMapper;

    @Override
    public String createLotteryFootballMatchSporttery(AppLotteryFootballMatchSportterySaveReqVO createReqVO) {
        // 插入
        LotteryFootballMatchSportteryDO lotteryFootballMatchSporttery = BeanUtils.toBean(createReqVO, LotteryFootballMatchSportteryDO.class);
        lotteryFootballMatchSportteryMapper.insert(lotteryFootballMatchSporttery);
        // 返回
        return lotteryFootballMatchSporttery.getMatchId();
    }

    @Override
    public void updateLotteryFootballMatchSporttery(AppLotteryFootballMatchSportterySaveReqVO updateReqVO) {
        // 校验存在
        validateLotteryFootballMatchSportteryExists(updateReqVO.getMatchId());
        // 更新
        LotteryFootballMatchSportteryDO updateObj = BeanUtils.toBean(updateReqVO, LotteryFootballMatchSportteryDO.class);
        lotteryFootballMatchSportteryMapper.updateById(updateObj);
    }

    @Override
    public void deleteLotteryFootballMatchSporttery(String id) {
        // 校验存在
        validateLotteryFootballMatchSportteryExists(id);
        // 删除
        lotteryFootballMatchSportteryMapper.deleteById(id);
    }

    private void validateLotteryFootballMatchSportteryExists(String id) {
        if (lotteryFootballMatchSportteryMapper.selectById(id) == null) {
            throw exception(LOTTERY_FOOTBALL_MATCH_SPORTTERY_NOT_EXISTS);
        }
    }

    @Override
    public LotteryFootballMatchSportteryDO getLotteryFootballMatchSporttery(String id) {
        return lotteryFootballMatchSportteryMapper.selectById(id);
    }

    @Override
    public PageResult<LotteryFootballMatchSportteryDO> getLotteryFootballMatchSportteryPage(AppLotteryFootballMatchSportteryPageReqVO pageReqVO) {
        return lotteryFootballMatchSportteryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LotteryFootballMatchSportteryDO> getSellingMatchList() {
        //由于竞猜网的状态字段问题，这里的重新整理一下,还没有奖金的比赛，就想不
        List<LotteryFootballMatchSportteryDO> list = lotteryFootballMatchSportteryMapper.selectList(new QueryWrapper<LotteryFootballMatchSportteryDO>().eq("match_status","Selling").orderByAsc("business_date"));
        List<LotteryFootballMatchSportteryDO> rlist=new ArrayList<>();
        //把当天的及未来的比赛先拿出来
        //保证当天未开始的比赛
        //在判断是否已经开盘，开盘的开始拿出来售卖
        LocalDateTime today = LocalDateTime.now();
        for(LotteryFootballMatchSportteryDO v:list){
            LocalDateTime businessDate = LocalDateTimeUtils.parse(v.getBusinessDate());
            //判断是否周末，周末23:00停手，其他22:00停手
            String stopTime="22:00";
            if(businessDate.getDayOfWeek()== DayOfWeek.SUNDAY||businessDate.getDayOfWeek()== DayOfWeek.SATURDAY){
                stopTime = "23:00";
            }
            String bt = "%s %s".formatted(v.getBusinessDate(),stopTime);
            LocalDateTime bus_time =  LocalDateTimeUtils.parseTime(bt);
            String t = "%s %s".formatted(v.getMatchDate(),v.getMatchTime());
            LocalDateTime match_time = LocalDateTimeUtils.parseTime(t);
            if(today.isBefore(bus_time)&&today.isBefore(match_time)){
                rlist.add(v);
            }
        }
        return rlist;
    }

}
